package orest.common.util;

import java.io.UnsupportedEncodingException;
import javax.servlet.http.HttpServletRequest;

public class EnCoding {
    public static String getParameter (HttpServletRequest request, String key,
            String from, String to) throws
            UnsupportedEncodingException {
        String value = request.getParameter (key);
        if (value == null) {
            return "";
        } else {
            return new String (value.trim ().getBytes (from), to);
        }
    }
    
    public static String getParameter (HttpServletRequest request, String key) throws
            UnsupportedEncodingException {
        return getParameter (request, key, "ISO-8859-1", "GBK");
    }
    
    public static String urlEncode (String s) {
        StringBuffer sb = new StringBuffer ();
        
        for (int i = 0; i < s.length (); ++i) {
            switch (s.charAt (i)) {
                case ' ':
                    sb.append ("%20");
                    break;
                    
                case '+':
                    sb.append ("%2b");
                    break;
                    
                case '\'':
                    sb.append ("%27");
                    break;
                    
                case '<':
                    sb.append ("%3c");
                    break;
                    
                case '>':
                    sb.append ("%3e");
                    break;
                    
                case '#':
                    sb.append ("%23");
                    break;
                    
                case '%':
                    sb.append ("%25");
                    break;
                    
                case '{':
                    sb.append ("%7b");
                    break;
                    
                case '}':
                    sb.append ("%7d");
                    break;
                    
                case '\\':
                    sb.append ("%5c");
                    break;
                    
                case '^':
                    sb.append ("%5e");
                    break;
                    
                case '~':
                    sb.append ("%73");
                    break;
                    
                case '[':
                    sb.append ("%5b");
                    break;
                    
                case ']':
                    sb.append ("%5d");
                    break;
                    
                default:
                    sb.append (s.charAt (i));
                    break;
            }
        }
        return sb.toString ();
    }
    
    /**
     *Encode   for   Javascript.
     */
    public static String jsEncoder (String   str) {
        if(str==null || str.equals (""))
            return "";
        String   res_str;
        res_str=strReplace (str,"'","\\'");
        res_str=strReplace (str,"\"","\\\"");
        res_str=strReplace (str,"\r\n","\\\n");
        res_str=strReplace (str,"\n","\\\n");
        res_str=strReplace (str,"\r","\\\n");
        return   res_str;
    }
    
    public static java.lang.String strReplace (String destStr, String oldStr, String newStr) {
        if(destStr==null)
            return "";
        String   tmpStr   =   destStr;
        int   foundPos   =   tmpStr.indexOf (oldStr);
        while   (foundPos>=0) {
            tmpStr   =   tmpStr.substring (0,foundPos)   +   newStr   +   tmpStr.substring (foundPos   +   oldStr.length (),tmpStr.length ());
            foundPos   =   tmpStr.indexOf (oldStr,foundPos+newStr.length ());
        }
        return tmpStr;
    }
    
    /**
     *Encode   for   HTML.
     */
    public static String htmlEncoder (String   str) {
        if(str==null   ||   str.equals (""))
            return   "";
        String res_str;
        res_str = strReplace (str,"<","&lt;");
        res_str = strReplace (str,">","&rt;");
        res_str = strReplace (str,"\"","&quot;");
        res_str = strReplace (str,"'","&#039;");
        return   res_str;
    }
    
    /**
     *Encode   for   HTML-Text.
     */
    public static String htmlTextEncoder (String   str) {
        if(str==null || str.equals (""))
            return   "";
        String   res_str;
        res_str=strReplace (str,"<","&lt;");
        res_str=strReplace (str,">","&rt;");
        res_str=strReplace (str,"\"","&quot;");
        res_str=strReplace (str,"'","&#039;");
        res_str=strReplace (str,"   ","&nbsp;");
        res_str=strReplace (str,"\r\n","<br>");
        res_str=strReplace (str,"\r","<br>");
        res_str=strReplace (str,"\n","<br>");
        return res_str;
    }
    
    /**
     *Encode for XML.
     */
    public static String xmlEncoder (String   str) {
        if(str==null || str.equals (""))
            return   "";
        String   res_str;
        res_str=strReplace (str,"&","&amp;");
        res_str=strReplace (res_str,"<","&lt;");
        res_str=strReplace (res_str,">","&gt;");
        res_str=strReplace (res_str,"\"",   "&quot;");
        res_str=strReplace (res_str,"\'",   "&acute;");
        return   res_str;
    }
    
    /**
     *Encode for XSLT.
     */
    public static String xslEncoder (String   str) {
        if(str==null || str.equals (""))
            return   "";
        String   res_str;
        res_str=strReplace (str,"&","&amp;");
        res_str=strReplace (res_str," ","&nbsp;");      
        res_str=strReplace (res_str,"\r\n","<br/>");
        res_str=strReplace (res_str,"\r","<br/>");
        res_str=strReplace (res_str,"\n",   "<br/>");
        res_str=strReplace (res_str,"\"",   "&quot;");
        res_str=strReplace (res_str,"\'",   "&acute;");
        return   res_str;
    }
    
    public static String xslEncoder1 (String   str) {
        if(str==null || str.equals (""))
            return   "";
        String   res_str;
        res_str=strReplace (str,"&amp;","&");
        res_str=strReplace (res_str,"&nbsp;"," ");      
        res_str=strReplace (res_str,"<br/>","\r\n");
        res_str=strReplace (res_str,"<br/>","\r");
        res_str=strReplace (res_str,"<br/>","\n");
        res_str=strReplace (res_str,"&quot;","\"");
        res_str=strReplace (res_str,"&acute;","\'");
        return   res_str;
    }
    
    /**
     *Encode   for   SQL.
     */
    public static String sqlEncoder (String   str) {
        if(str==null || str.equals (""))
            return   "";
        String   res_str;
        res_str=strReplace (str,"'","''");
        return   res_str;
    }
    
    public static String base64encoding (String input) {
        String keyStr = "ABCDEFGHIJKLMNOP" +
                        "QRSTUVWXYZabcdef" +
                        "ghijklmnopqrstuv" +
                        "wxyz0123456789+/" +
                        "=";
        String output = "";
        char chr1, chr2, chr3;
        char enc1, enc2, enc3, enc4;
        int i = 0;
        
        do {
            chr1 = input.charAt (i++);
            chr2 = input.charAt (i++);
            chr3 = input.charAt (i++);
            
            enc1 = (char)(chr1 >> 2);
            enc2 = (char)(((chr1 & 3) << 4) | (chr2 >> 4));
            enc3 = (char)(((chr2 & 15) << 2) | (chr3 >> 6));
            enc4 = (char)(chr3 & 63);
            
            if (java.lang.Character.isDigit (chr2)) {
                enc3 = enc4 = 64;
            } else if (java.lang.Character.isDigit(chr3)) {
                enc4 = 64;
            }
            
            output = output +
                    keyStr.charAt (enc1) +
                    keyStr.charAt (enc2) +
                    keyStr.charAt (enc3) +
                    keyStr.charAt (enc4);
            chr1 = chr2 = chr3 = 0;
            enc1 = enc2 = enc3 = enc4 = 0;
        } while (i < input.length ());
        
        return output;
    }

    public String xslEncoder(Tools tool, String parseNILL) {
        throw new UnsupportedOperationException("Not yet implemented");
    }

    public String xslEncoder1(Tools tool, String parseNILL) {
        throw new UnsupportedOperationException("Not yet implemented");
    }
    
    
}